package com.shaoxi.ext.framework.impl;

import com.shaoxi.ext.framework.ExtManager;
import com.shaoxi.ext.framework.IBusinessExt;
import com.shaoxi.ext.framework.anotation.Ext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 自动注册扩展实现
 */
@Component
public class AutoRegisterExt implements  BeanPostProcessor {

    @Resource
    private ExtManager extManager;

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof IBusinessExt){
            IBusinessExt businessExt = (IBusinessExt)bean;
            Ext ext = bean.getClass().getAnnotation(Ext.class);
            if(ext==null){
                extManager.register(IBusinessExt.DEFAULT, businessExt);
            }else {
               for(String bizCode : ext.suportBizCode()){
                   extManager.register(bizCode, businessExt);
               }
            }
        }
        return bean;
    }

}
